const tencentcloud = require("tencentcloud-sdk-nodejs");

const IotcloudClient = tencentcloud.iotcloud.v20180614.Client;
const models = tencentcloud.iotcloud.v20180614.Models;
const Credential = tencentcloud.common.Credential;
const ClientProfile = tencentcloud.common.ClientProfile;
const HttpProfile = tencentcloud.common.HttpProfile;

function GetIotcloudClient(SecretId: string, SecretKey: string) {
    let cred = new Credential(SecretId, SecretKey);
    let httpProfile = new HttpProfile();
    httpProfile.endpoint = "iotcloud.tencentcloudapi.com";
    let clientProfile = new ClientProfile();
    clientProfile.httpProfile = httpProfile;
    let client = new IotcloudClient(cred, "ap-guangzhou", clientProfile);
    return client
}

export function PublishMessage(SecretId: string, SecretKey: string, Params: PublishMessageParams): Promise<any> {
    return new Promise((resolve, reject) => {
        let client = GetIotcloudClient(SecretId, SecretKey);
        let req = new models.PublishMessageRequest();
        let params = JSON.stringify(Params)
        req.from_json_string(params);
        client.PublishMessage(req, function (errMsg: any, response: any) {
            if (errMsg) {
                reject(errMsg);
                return;
            }
            resolve(response)
        });
    })
}

export function CreateDevice(SecretId: string, SecretKey: string, Params: CreateDeviceParams): Promise<any> {
    return new Promise((resolve, reject) => {
        let client = GetIotcloudClient(SecretId, SecretKey);
        let req = new models.CreateDeviceRequest();
        let params = JSON.stringify(Params)
        req.from_json_string(params);

        client.CreateDevice(req, function (errMsg: any, response: any) {
            if (errMsg) {
                reject(errMsg);
                return;
            }
            resolve(response);
        });
    })
}

export function DescribeDevice(SecretId: string, SecretKey: string, Params: DescribeDeviceParams): Promise<any> {
    return new Promise((resolve, reject) => {
        let client = GetIotcloudClient(SecretId, SecretKey);
        let req = new models.DescribeDeviceRequest();
        let params = JSON.stringify(Params)
        req.from_json_string(params);

        client.DescribeDevice(req, function (errMsg: any, response: any) {
            if (errMsg) {
                reject(errMsg);
                return;
            }
            resolve(response);
        });
    })
}

export function DescribeDeviceShadow(SecretId: string, SecretKey: string, Params: DescribeDeviceShadowParams): Promise<any> {
    return new Promise((resolve, reject) => {
        let client = GetIotcloudClient(SecretId, SecretKey);
        let req = new models.DescribeDeviceShadowRequest();
        let params = JSON.stringify(Params)
        req.from_json_string(params);

        client.DescribeDeviceShadow(req, function (errMsg: any, response: any) {
            if (errMsg) {
                reject(errMsg);
                return;
            }
            resolve(response);
        });
    })
}

export function CreateTask(SecretId: string, SecretKey: string, Params: CreateTaskParams): Promise<any> {
    return new Promise((resolve, reject) => {
        let client = GetIotcloudClient(SecretId, SecretKey);
        let req = new models.CreateTaskRequest();
        let params = JSON.stringify(Params)
        req.from_json_string(params);

        client.CreateTask(req, function (errMsg: any, response: any) {
            if (errMsg) {
                reject(errMsg);
                return;
            }
            resolve(response);
        });
    })
}


export function DescribeTask(SecretId: string, SecretKey: string, Params: TaskIdParams): Promise<any> {
    return new Promise((resolve, reject) => {
        let client = GetIotcloudClient(SecretId, SecretKey);
        let req = new models.DescribeTaskRequest();
        let params = JSON.stringify(Params)
        req.from_json_string(params);

        client.DescribeTask(req, function (errMsg: any, response: any) {
            if (errMsg) {
                reject(errMsg);
                return;
            }
            resolve(response);
        });
    })
}

export function CancelTask(SecretId: string, SecretKey: string, Params: TaskIdParams): Promise<any> {
    return new Promise((resolve, reject) => {
        let client = GetIotcloudClient(SecretId, SecretKey);
        let req = new models.CancelTaskRequest();
        let params = JSON.stringify(Params)
        req.from_json_string(params);

        client.CancelTask(req, function (errMsg: any, response: any) {
            if (errMsg) {
                reject(errMsg);
                return;
            }
            resolve(response);
        });
    })
}
